package ro.thich.alchemy;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class AddElement extends Activity implements OnClickListener
 {

	// GUI components
	private EditText todoText;        // Text field
	private Button addNewButton;    // Add new button
	private Button backButton;        // Back button

	// DAO
	private ElementDAO dao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_element);

		// Create DAO object
		dao = new ElementDAO(this);

		todoText         = (EditText)findViewById(R.id.newElementText);
		addNewButton     = (Button)findViewById(R.id.addNewelementButton);
		backButton        = (Button)findViewById(R.id.menuGoBackButton);

		addNewButton.setOnClickListener(this);
		backButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// If add button was clicked
		if (addNewButton.isPressed()) {
			// Get entered text
			String todoTextValue = todoText.getText().toString();
			todoText.setText("");

			// Add text to the database
			dao.createTodo(todoTextValue);

			// Display success information
			Toast.makeText(getApplicationContext(), R.string.newelem, Toast.LENGTH_LONG).show();

		} else if (backButton.isPressed()) {
			// When back button is pressed
			// Create an intent
			Intent intent = new Intent(this, MainActivity.class);
			// Start activity
			startActivity(intent);
			// Finish this activity
			this.finish();

			// Close the database
			dao.close();
		}

	}
}
